<?php

namespace app\data\controller\api;

use app\data\service\ExpressService;
use app\data\service\GoodsService;
use think\admin\Controller;

/**
 * 商品数据接口
 * Class Goods
 * @package app\data\controller\api
 */
class Goods extends Controller
{
    /**
     * 获取分类数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCate()
    {
        $this->success('获取分类成功', GoodsService::instance()->getCateTree('arr2tree', input('module', 0)));
    }

    /**
     * 获取标签数据
     */
    public function getMark()
    {
        $this->success('获取标签成功', GoodsService::instance()->getMarkData());
    }

    /**
     * 获取商品数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGoods()
    {
        // 更新访问统计
        $map = $this->_vali(['code.default' => '']);
        if ($map['code']) $this->app->db->name('ShopGoods')->where($map)->inc('num_read')->update();
        // 商品数据处理
        $query = $this->_query('ShopGoods')->like('name,marks,payment')->equal('code,type,state_home');
        if ($priceSelling = input('price_selling', '')) {
            [$begin, $after] = explode(',', $priceSelling);
            if ($begin && $after) {
                $query = $query->whereBetween('price_selling', [$begin, $after]);
            } else if ($begin) {
                $query = $query->where('price_selling', '>=', $begin);
            } else if ($after) {
                $query = $query->where('price_selling', '<=', $after);
            }
        }
        if ($cateIds = input('cate_id', '')) {
            $cateIds = explode(',', $cateIds);
            $ids = [];
            foreach ($cateIds as $cateId) {
                $cIds = $this->app->db->name('ShopGoods')->whereLike('cateids', "%,{$cateId},%")->column('id');
                $ids = array_merge($ids, $cIds);
            }
            $query = $query->whereIn('id', $ids);
        }
        $result = $query->where(['deleted' => 0, 'status' => 1, 'module' => 0])->order(input('order', 'id desc'))->page(true, false, false, input('limit', 15));
        if (count($result['list']) > 0) GoodsService::instance()->bindData($result['list']);
        $this->success('获取商品数据', $result);
    }

    /**
     * 获取商品详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGoodsDetail()
    {
        // 更新访问统计
        $map = $this->_vali(['code.default' => '']);
        if ($map['code']) $this->app->db->name('ShopGoods')->where($map)->inc('num_read')->update();
        // 商品数据处理
        $query = $this->app->db->name('ShopGoods')->where('code', $map['code']);
        $result = $query->where(['deleted' => 0, 'status' => 1])->find();
        $list = [$result];
        GoodsService::instance()->bindData($list);
        $list[0]['month_stock_sales'] = GoodsService::instance()->getMonthStockSales($map['code']);
        $this->success('获取商品数据', $list[0]);
    }

    /**
     *  获取配送区域
     */
    public function getRegion()
    {
        $this->success('获取区域成功', ExpressService::instance()->region(3, 1));
    }

}